package cn.web.web_module.controller;

import cn.itcast.util.ResponseEntity;
import cn.wx.MyConfig;
import cn.wx.MyConfig2;
import cn.wx.WXPay;
import cn.wx.WXPayUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 16:15 2021/2/21
 * @Modified By:
 */
@RestController
@RequestMapping("/WxpayTest")
public class WxPayTest {

    @PostMapping("/wxpay")
    public ResponseEntity Wxpay(){
        Map<String,String> map = new HashMap<>();
        Map<String,String> returnMap = new HashMap<>();
        WXPay wxPay=null;
        try {
            wxPay = new WXPay(new MyConfig2());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String spbill_create_ip=null;
        try {
            spbill_create_ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        LocalDate localDate = LocalDate.now();
        Timestamp timestamp= Timestamp.valueOf(LocalDateTime.now());
        map.put("body","立即充值");
        map.put("out_trade_no","20210222154"+timestamp.getTime());//商户订单号
        map.put("total_fee","1");
        map.put("spbill_create_ip",spbill_create_ip);
        map.put("notify_url","http://6ituns.natappfree.cc/WxpayTest/notifyUrl");
        map.put("trade_type","JSAPI");
        map.put("openid","osdn25dtBEQ_Sx0yrH2dneMg5iFM");
        map.put("attach","openiddate300d:"+Math.random()*100000);
        Map<String, String> stringStringMap=null;
        try {
            stringStringMap = wxPay.unifiedOrder(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("map:"+map);
        System.out.println("stringStringMap:"+stringStringMap);
        returnMap.put("appId",stringStringMap.get("appid"));
        returnMap.put("timeStamp",timestamp.getTime()+"");
        returnMap.put("nonceStr",WXPayUtil.generateNonceStr());
        returnMap.put("package","prepay_id="+stringStringMap.get("prepay_id"));
        returnMap.put("signType","MD5");
        String paySign=null;
        try {
            paySign = WXPayUtil.generateSignature(returnMap, new MyConfig().getKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
        returnMap.put("paySign",paySign);
        returnMap.remove("appId");
        return new ResponseEntity(returnMap);
    }

    @RequestMapping("notifyUrl")
    public void notifyUrl(HttpServletRequest request){
        String postStr = WXPayUtil.getPostStr(request);
        System.out.println("postStr:"+postStr);
        System.out.println("+++++++++++++++++++++++++++++");
    }



}
